package menu.view 
{
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import interfaces.IOperate;
	import menu.MenuC;
	import org.airmvc.Dispatcher;
	
	/**
	 * ...
	 * @author WLDragon 2014-03-18
	 */
	public class NumberMenu extends Sprite implements IOperate 
	{
		
		public function NumberMenu() 
		{
			super();
			itemPanel = new Sprite();
			addChild(itemPanel);
		}
		
		/* INTERFACE interfaces.IOperate */
		
		public function execute(keyCode:int):void 
		{
			var v:Number;
			switch (keyCode) 
			{
				case Keyboard.A:
					curItem.index--;
				break;
				case Keyboard.D:
					curItem.index++;
				break;
				case Keyboard.W:
					v = curItem.length - 1 - curItem.index;
					curItem.value += Math.pow(10,v);
				break;
				case Keyboard.S:
					v = curItem.length - 1 - curItem.index;
					var t:int = Math.pow(10,v);
					if(curItem.value >= t)
						curItem.value -= t;
				break;
				case Keyboard.SPACE:
					if (index < itemPanel.numChildren - 1)
					{
						index++;
					}
					else
					{
						var d:Array = [];
						for (var i:int = 0; i < itemPanel.numChildren; i++) 
						{
							var m:NumberItem = itemPanel.getChildAt(i) as NumberItem;
							d.push(m.value);
						}
						Dispatcher.send(MenuC.VERIFY_PARAM, this, d);
					}
				break;
				case Keyboard.R:
					if (index > 0)
						index--;
					else
						Dispatcher.send(MenuC.EXE_ORDER, this, backMenu);
				break;
			}
		}
		
		/**
		 * @param data [{label:,max:,len:,minLen:},...]
		 */
		public function update(data:Array,backMenu:int,tx:int,ty:int):void
		{
			this.x = tx;
			this.y = ty;
			this.backMenu = backMenu;
			while (itemPanel.numChildren > 0)
			{
				itemPanel.removeChildAt(0);
			}
				
			for (var i:int = 0,len:int = data.length; i < len;i++ )
			{
				var n:NumberItem = new NumberItem(data[i].label, data[i].max, data[i].len,data[i].minLen);
				n.x = 140 * (i % 2);
				n.y = 34 * (i / 2 >> 0);
				itemPanel.addChild(n);
			}
			
			index = 0;
			Kyb.setOperater(this);
		}
		
		private function get index():int 
		{
			return _index;
		}
		
		private function set index(value:int):void 
		{
			if (curItem)
				curItem.unActive();
				
			curItem = itemPanel.getChildAt(value) as NumberItem;
			curItem.active();
			_index = value;
		}
		
		private var itemPanel:Sprite;
		private var curItem:NumberItem;
		private var _index:int;
		/**返回的菜单序号*/
		private var backMenu:int;
	}

}